package indi.h.filefilter.lists;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class NRPArrayList<E extends NRPArrayList.CheckField> implements NRPList<E> {
	private List<E> list_data;
	private Map<Object, Integer> pos_re;

	public NRPArrayList() {
		pos_re = new HashMap<Object, Integer>();
		list_data = new ArrayList<E>();
	}

	public interface CheckField {
		public Object getCF();
	}

	public void put(E e) {
		// TODO Auto-generated method stub
		if (pos_re.get(e.getCF()) != null) {
			list_data.add(pos_re.get(e.getCF()), e);
		} else {
			pos_re.put(e.getCF(), list_data.size());
			list_data.add(e);
		}
	}

	private void resetPos(Integer i) {
		for (Entry<Object, Integer> m : pos_re.entrySet()) {
			if (m.getValue() > i) {
				pos_re.put(m.getKey(), (m.getValue() - 1));
			}
		}
	}

	public void del(E e) {
		Integer integer = pos_re.get(e.getCF());
		pos_re.remove(e.getCF());
		list_data.remove(integer);
		resetPos(integer);
	}

	@Override
	public int size() {
		return this.list_data.size();
	}

	@Override
	public E get(int i) {
		// TODO Auto-generated method stub
		return this.list_data.get(i);
	}

}
